package controllers

import (
	"gin-worked/app/system/service"
	"gin-worked/app/system/validate"
	"gin-worked/work"
	"github.com/gin-gonic/gin"
)

type MenuController struct {
}

// ListTree 获取菜单树形结构
func (m MenuController) ListTree(c *gin.Context) {
	uid := c.MustGet("uid").(int)
	res := service.ListMenuTree(uid)
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

func (m MenuController) List(c *gin.Context) {
	res := service.ListMenu()
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Add 菜单添加
func (m MenuController) Add(c *gin.Context) {

	//fmt.Println(123)
	var add validate.MenuAdd
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)

	res := service.MenuAdd(param["Name"], param["IsMenu"], param["Url"], param["Sort"], param["Status"], param["ParentId"], param["Icon"], param["WebUrl"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Info 获取菜单详情
func (m MenuController) Info(c *gin.Context) {
	var add validate.MenuInfo
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.MenuInfo(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Edit Add 菜单编辑
func (m MenuController) Edit(c *gin.Context) {
	var add validate.MenuEdit
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)

	res := service.MenuEdit(param["Id"], param["Name"], param["IsMenu"], param["Url"], param["Sort"], param["Status"], param["ParentId"], param["Icon"], param["WebUrl"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// SetStatus 设置菜单状态
func (m MenuController) SetStatus(c *gin.Context) {
	var add validate.MenuInfo
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.MenuSetStatus(param["Id"], param["Status"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Del SetStatus 删除菜单
func (m MenuController) Del(c *gin.Context) {
	var add validate.MenuInfo
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.MenuDel(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}
